<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>map对象</title>
</head>

<body>

</body>
<script>
  /*
  根据type将相同的数据分组
  */
  let arr = [{
    id: 1,
    name: '小明',
    type: '1',
    typeName: '分类1'
  },
  {
    id: 2,
    name: '大明',
    type: '1',
    typeName: '分类1'
  },
  {
    id: 3,
    name: '中明',
    type: '2',
    typeName: '分类2'

  },
  {
    id: 4,
    name: '小明',
    type: '3',
    typeName: '分类3'
  },
  {
    id: 5,
    name: '雷明',
    type: '3',
    typeName: '分类3'

  },
  {
    id: 6,
    name: '小明',
    type: '3',
    typeName: '分类3'
  },
  {
    id: 7,
    name: '出明',
    type: '2',
    typeName: '分类2'
  },
  {
    id: 8,
    name: '中明',
    type: '1',
    typeName: '分类1'
  },
  {
    id: 9,
    name: '出明',
    type: '4',
    typeName: '分类4'
  },
  {
    id: 10,
    name: '雷明',
    type: '4',
    typeName: '分类4'
  },
  ];
  function sortClass(sortData) {
    const groupBy = (array, f) => {
      let groups = {};
      array.forEach((o) => {
        let group = JSON.stringify(f(o));
        groups[group] = groups[group] || [];
        groups[group].push(o);
      });
      return Object.keys(groups).map((group) => {
        return groups[group];
      });
    };
    const sorted = groupBy(sortData, (item) => {
      return item.type; // 返回需要分组的对象
    });
    return sorted;
  };
  let a = sortClass(arr);
  console.log('a', a)



  // -----------------------------------
  var map = {},
    bbbb = [];
  for (var i = 0; i < arr.length; i++) {
    var ai = arr[i];
    if (!map[ai.type]) {
      bbbb.push({
        type: ai.type,
        name: ai.name,
        data: [ai]
      });
      map[ai.type] = ai;
    } else {
      for (var j = 0; j < bbbb.length; j++) {
        var dj = bbbb[j];
        if (dj.type == ai.type) {
          dj.data.push(ai);
          break;
        }
      }
    }
  }
  console.log('bbbb', bbbb);


  // -----------------------------------
  let list = [{
    name: '张三',
    id: 1
  },
  {
    name: '李四',
    id: 2
  },
  {
    name: '王五',
    id: 3
  },
  {
    name: '张三',
    id: 4
  },
  {
    name: '李四',
    id: 5
  }
  ];

  let cccc = []
  list.forEach(item => {
    let newName = cccc.find((i) => i.name == item.name)
    if (!newName) {
      cccc.push({
        name: item.name,
        ids: [item.id]
      })
    } else {
      newName.ids.push(item.id)
    }
  })
  console.log('cccc', cccc)
  // ------------------------在JavaScript中对相同数字进行分组
  const arr1 = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];
  const pcard = arr1 => {
    return arr1.reduce((acc, val, ind) => {
      if (val >= 0 && (arr1[ind + 1] < 0 || typeof arr1[ind + 1] === 'undefined')) {
        acc++;
      };
      return acc;
    }, 0);
  };
  // console.log(pcard(arr1));
  // 二、TS 写法
  // const sortClass = (sortData: ListDataItem[]) => {
  //   const groupBy = (array: ListDataItem[], f: Function) => {
  //     const groups: {
  //       [key: string]: ListDataItem[],
  //     } = {};
  //     array.forEach((item) => {
  //       const group = JSON.stringify(f(item));
  //       groups[group] = groups[group] || [];
  //       groups[group].push(item);
  //     });
  //     return Object.keys(groups).map((group) => {
  //       return groups[group];
  //     });
  //   };
  //   const sorted = groupBy(sortData, (item: ListDataItem) => {
  //     return item.lastName;
  //   });
  //   return sorted;
  // };
</script>

</html>